package com.xuyaxu.service.impl;

import com.xuyaxu.service.MinioService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 类的描述
 *
 * @author xuyaxu
 * @date 2021/6/15 18:02
 */
@Service
public class MinioServiceImpl implements MinioService {
    @Resource
    private MinioClient minioClient;

    @Override
    public void minioUpload(MultipartFile file) {
        // 上传的文件的名称
        String filename = file.getOriginalFilename();
        try {                                                      // 服务器端存储的文件名
            PutObjectArgs putObjectArgs = PutObjectArgs.builder().object(file.getOriginalFilename())
                    .bucket("my-minio-bucket")
                    .contentType(file.getContentType())
                    .stream(file.getInputStream(), file.getSize(), -1).build();
            minioClient.putObject(putObjectArgs);
            String url = "http://192.168.216.129:9000/my-minio-bucket/" + filename;
            System.out.println("文件地址：" + url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
